//
//  YDFibonacciSequence.swift
//  YDAlgorithm
//
//  Created by wangyd on 2020/11/10.
//

import UIKit

class YDFibonacciSequence: NSObject {

    
    func fibRec(num: Double) -> Double {
        
        if num < 1 {
            return 0
        }
        if num < 3 {
            return 1
        }
        
        return fibRec(num: num - 1) + fibRec(num: num - 2)
    }

    func fibRec2(num: Double) -> Double {
        var num1: Double = 0
        var num2: Double = 1
        var num3: Double = 1
        
        for _ in 2...Int(num) {
            num1 = num2
            num2 = num3
            num3 = num1 + num2
        }
        return num2
    }
    
    func fibonacciSequence(num: Int) {
        
        print("wuyezhiguhun - FibonacciSequence")
        
        let startAbsolutTime = CFAbsoluteTimeGetCurrent()
        
        let startDate = Date()
        
        
        let number = fibRec2(num: Double(num))
    
        
        print("FibonacciSequence: \(number)")
        
        let endAbsolutTime = CFAbsoluteTimeGetCurrent()
        
        let endDate = Date()
        
        
        print("TimeDifference: \(endAbsolutTime - startAbsolutTime)")
        
        print("DateDifference: \(endDate.timeIntervalSince(startDate))")
        
    }
    
}
